package com.ztjava.core.tool;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.redis.Cache;
import com.jfinal.plugin.redis.Redis;
import com.ztjava.core.Constant;

public class FlowTool {
	
	public static JSONObject findNode(String code,String node){
		Cache cache = Redis.use();
		JSONObject flow = JSONObject.parseObject(cache.hget(Constant.R_FLOW, code).toString());
		if(flow!=null){
			JSONObject nodes = flow.getJSONObject("nodes");
			return nodes.getJSONObject(node);
		}
		return null;
	}
	
	public static JSONArray findNoEndNode(String code){
		Cache cache = Redis.use();
		JSONObject flow = JSONObject.parseObject(cache.hget(Constant.R_FLOW,code).toString());
		if(flow==null) return new JSONArray();
		JSONObject nodes = flow.getJSONObject("nodes");
		JSONArray nodeList = new JSONArray();
		for(String key:nodes.keySet()){
			JSONObject node = nodes.getJSONObject(key);
			if(node.getString("type").equals("end round")) {
				continue;
			}
			node.put("id", key);
			nodeList.add(node);
		}
		return nodeList;
	}
	
	public static JSONArray findNextNode (String code,String now_node){
		JSONArray nodeList = new JSONArray();
		Cache cache = Redis.use();
		JSONObject flow = JSONObject.parseObject(cache.hget(Constant.R_FLOW, code).toString());
		if(flow!=null){
			JSONObject nodes = flow.getJSONObject("nodes");
			if(now_node==null||now_node.equals("")){
				for(String key:nodes.keySet()){
					JSONObject node = nodes.getJSONObject(key);
					if(node.getString("type").equals("start round")) {
						node.put("id", key);
						nodeList.add(node);
					}
				}
			}else {
				JSONObject lines = flow.getJSONObject("lines");
				List<String> nextList = new ArrayList<>();
				for(String key:lines.keySet()){
					JSONObject line = lines.getJSONObject(key);
					if(line.getString("from").equals(now_node)){
						nextList.add(line.getString("to"));
					}
				}
				for(int i=0;i<nextList.size();i++){
					JSONObject item = nodes.getJSONObject(nextList.get(i));
					item.put("id", nextList.get(i));
					nodeList.add(item);
				}
			}
		}
		return nodeList;
	}

	
	public static void change(List<?> list,String[]... field){
		if(field==null||field.length==0){
			return;
		}
		String value;
		Cache cache = Redis.use();
		JSONObject flow = null;
		String code = null;
		
		JSONObject nodes = null;
		for(int i=0;i<list.size();i++){
			code = ((Model<?>)list.get(i)).getStr("flow_code");
			flow = JSONObject.parseObject(cache.hget(Constant.R_FLOW, code).toString());
			if(flow==null) continue;
			nodes = flow.getJSONObject("nodes");
			for(String[] item:field){
				value = ((Model<?>)list.get(i)).get(item[0]);
				if(value==null) continue;
				if(nodes.getJSONObject(value)==null){
					((Model<?>)list.get(i)).put(item[0]+"_"+item[1], value+"(已过期)");
					continue;
				}
				((Model<?>)list.get(i)).put(item[0]+"_"+item[1], nodes.getJSONObject(value).getString(item[1]));
			}
		}
	}
	public static void change(List<?> list,String code,String[]... field){
		if(field==null||field.length==0){
			return;
		}
		String value;
		Cache cache = Redis.use();
		JSONObject flow = JSONObject.parseObject(cache.hget(Constant.R_FLOW, code).toString());
		if(flow==null) return;
		JSONObject nodes = flow.getJSONObject("nodes");
		for(int i=0;i<list.size();i++){
			for(String[] item:field){
				value = ((Model<?>)list.get(i)).get(item[0]);
				if(value==null) continue;
				if(nodes.getJSONObject(value)==null){
					((Model<?>)list.get(i)).put(item[0]+"_"+item[1], value+"(已过期)");
					continue;
				}
				((Model<?>)list.get(i)).put(item[0]+"_"+item[1], nodes.getJSONObject(value).getString(item[1]));
			}
		}
	}
}
